package com.passncool.testbed.network;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class WhoisClient
{
    public final static int DEFAULT_PORT = 43;
    public final static String DEFAULT_HOST = "whois.internic.net";
    
    public static void main(String[] args)
    {
       
        String serverName = System.getProperty("WHOIS_SERVER",DEFAULT_HOST);

        InetAddress serverAddress = null;
        
        try
        {
            serverAddress = InetAddress.getByName(serverName);
            
            Socket sock = new Socket(serverAddress, DEFAULT_PORT);
            
            Writer out = new OutputStreamWriter(sock.getOutputStream(), "8859_1");
            
            out.write("passncool");
            out.write("\r\n");
            out.flush();
            
            InputStream raw = sock.getInputStream();
            InputStream in = new BufferedInputStream(raw);
            
            StringBuffer sb = new StringBuffer();
            
            int c;
            
            while((c = in.read()) != -1)
            {
               sb.append((char)c);
            }
            
            System.out.println(sb.toString());
            
        }
        catch (UnknownHostException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }

}
